package org.exhaust.framework.net.client.tcp.handler;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.exhaust.framework.net.client.tcp.TcpClient;
import org.exhaust.framework.net.common.protocol.ChannelEventHandlerAdapter;
import org.exhaust.framework.net.common.protocol.RunningState;

@Slf4j
public class TcpReconnectChannelEventHandler extends ChannelEventHandlerAdapter {

    private TcpClient tcpClient;

    public TcpReconnectChannelEventHandler(TcpClient tcpClient) {
        this.tcpClient = tcpClient;
    }

    @Override
    public void channelInActive(Channel channel) {
        if (this.tcpClient.getState() == RunningState.RUNNING) {
            log.error("连接中断,即将进行断线重连:{}", channel.remoteAddress());
            tcpClient.connect();
        }
    }

}
